package models

import (
	"LoveU/utils"
	"time"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

// 登录dao操作
// 状态码表示如下
// utils.OK：登陆成功
// utils.LOGIN_PWDWRONG：用户密码错误
// utils.LOGIN_USRWRONG：用户不存在
func AdminLogin(name, passwd string) int64 {
	o := orm.NewOrm()
	qs := o.QueryTable("admin")
	user := new(User)
	err := qs.Filter("name", name).One(user)
	if err != nil {
		//用户不存在
		if user.Name == "" {
			return utils.LOGIN_USRWRONG
		}
	} else {
		//密码不正确
		if user.Password != passwd {
			return utils.LOGIN_PWDWRONG
		}
	}
	return utils.OK
}

// 获取一个用户对象
// 返回nil说明不正确的登录状态
func GetAdmin(name, passwd string) *Admin {
	o := orm.NewOrm()
	qs := o.QueryTable("admin")
	admin := new(Admin)
	err := qs.Filter("name", name).One(admin)
	if err != nil {
		//用户不存在
		if admin.Name == "" {
			beego.Error("用户不存在")
			return nil
		}
	} else {
		//密码不正确
		if admin.Password != passwd {
			beego.Error(admin.Password, passwd)
			beego.Error("密码不正确")
			return nil
		}
	}
	return admin
}

// 根据用户名删除特定用户
func DeleteUser(name string) int64 {
	o := orm.NewOrm()
	qs := o.QueryTable("user")
	num, err := qs.Filter("name", name).Delete()
	if num == 1 && err == nil {
		return utils.OK
	}
	return utils.USER_NOT_FOUND
}

//冻结用户账号
func FrozenUser(name string) int64 {
	o := orm.NewOrm()
	qs := o.QueryTable("user")
	user := new(User)
	err := qs.Filter("name", name).One(user)
	if err != nil {
		return utils.USER_NOT_FOUND
	}
	user.Frozen = true
	o.Update(user)
	return utils.OK
}

//获取活动圈信息
func GetActivities(page, pagesize int64) (*utils.Pagination, int64) {
	o := orm.NewOrm()
	qs := o.QueryTable("activity")
	activities := make([]*Activity, 0)
	countAll, err0 := qs.Count()
	startLine := (page - 1) * pagesize
	sum, _ := qs.Exclude("delete", true).Count()
	_, err := qs.OrderBy("data").Exclude("delete", true).Limit(pagesize).Offset(startLine).All(&activities)
	if (err0 != nil && err0 != orm.ErrNoRows) || err != nil {
		beego.Error(err.Error())
		return nil, utils.UNKNOWN_ERROR
	}
	var pagenum int64
	if countAll%pagesize == 0 {
		pagenum = sum / pagesize
	} else {
		pagenum = sum/pagesize + 1
	}
	data := &utils.Pagination{
		Page:      page,
		Pagesize:  pagesize,
		Pagecount: pagenum,
		Sum:       sum,
		Data:      activities,
	}
	return data, utils.OK
}

// 返回VIP申请List
func GetVIPList(page, pagesize int64) (*utils.Pagination, int64) {
	o := orm.NewOrm()
	qs := o.QueryTable("viprequest").Exclude("agree", 2)
	requests := make([]*Viprequest, 0)
	countAll, err0 := qs.Count()
	startLine := (page - 1) * pagesize
	sum, err := qs.Limit(pagesize).Offset(startLine).All(&requests)
	if (err0 != nil && err0 != orm.ErrNoRows) || err != nil {
		beego.Error(err.Error())
		return nil, utils.UNKNOWN_ERROR
	}
	var pagenum int64
	if countAll%pagesize == 0 {
		pagenum = countAll / pagesize
	} else {
		pagenum = countAll/pagesize + 1
	}
	list := make([]*UserRequestedVip, 0)
	for _, request := range requests {
		info := new(UserRequestedVip)
		user := GetUser2(request.Userid)
		if user == nil {
			info.Name = ""
		} else {
			info.Name = user.Name
			info.userid = user.Id
			info.Id = request.Id
		}
		// info.Id = user.Id
		info.Avatar = GetUserAvatar(user.Id)
		info.Status = request.Agree
		list = append(list, info)
	}
	data := &utils.Pagination{
		Page:      page,
		Pagesize:  pagesize,
		Pagecount: pagenum,
		Sum:       sum,
		Data:      list,
	}
	return data, utils.OK
}

// 返回某用户的VIP申请资料
func GetVipRequest(userid int64) *AdminVIPReturn {
	adminVipReturn := new(AdminVIPReturn)
	o := orm.NewOrm()
	qs := o.QueryTable("viprequest")
	request := new(Viprequest)
	err := qs.Filter("userid", userid).One(request)
	if err != nil {
		beego.Error("error 173")
		return nil
	}

	qs = o.QueryTable("pinformation")
	pInfo := new(Pinformation)
	err = qs.Filter("userid", userid).One(pInfo)
	if err != nil {
		beego.Error("error 181")
		// return nil
	}

	qs = o.QueryTable("user")
	user := new(User)
	err = qs.Filter("id", userid).One(user)
	if err != nil {
		beego.Error("error 189")
		return nil
	}

	adminVipReturn.Vip = request
	adminVipReturn.PInfo = pInfo
	adminVipReturn.Name = user.Name
	adminVipReturn.Userid = user.Id
	return adminVipReturn
}

// 同意申请
// agree参数：
// 1：同意
// 2：反对
func AgreeVipRequest(id, agree int64, reason, url1, url2, url3 string) int64 {
	o := orm.NewOrm()
	qs := o.QueryTable("viprequest")
	request := new(Viprequest)
	err := qs.Filter("id", id).Filter("agree", 0).One(request)
	if err != nil {
		return utils.UNKNOWN_ERROR
	}
	request.Agree = agree
	request.Refusereason = reason
	request.Picurl1 = url1
	request.Picurl2 = url2
	request.Picurl3 = url3
	request.Useless = true
	o.Update(request)
	qs = o.QueryTable("Pinformation")
	pInfo := new(Pinformation)
	err = qs.Filter("userid", request.Userid).One(pInfo)
	if err != nil {
		return utils.UNKNOWN_ERROR
	}
	pInfo.Vip = true
	o.Update(pInfo)
	return utils.OK

}

// 发布活动
func PublishActivity(content string, adminid int64) int64 {
	o := orm.NewOrm()
	activity := &Activity{
		Content: content,
		Data:    time.Now(),
		Adminid: adminid,
	}
	_, err := o.Insert(activity)
	if err != nil {
		return utils.UNKNOWN_ERROR
	}
	return utils.OK
}

// 删除活动
func DeleteActivity(adminid, id int64) int64 {
	o := orm.NewOrm()
	qs := o.QueryTable("activity")
	activity := new(Activity)
	err := qs.Filter("adminid", adminid).Filter("id", id).One(activity)
	if err != nil {
		return utils.UNKNOWN_ERROR
	}
	activity.Content = ""
	activity.Delete = true
	o.Update(activity)
	return utils.OK
}
